package com.lzh.dlykserver.controller;

import com.github.pagehelper.PageInfo;
import com.lzh.dlykserver.entity.TClue;
import com.lzh.dlykserver.query.ClueQuery;
import com.lzh.dlykserver.result.R;
import com.lzh.dlykserver.service.Clueservice;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
public class ClueController {

    @Resource
    private Clueservice clueService;

    /**
     * 线索分页
     * @param current
     * @return
     */
    @PreAuthorize(value = "hasAnyAuthority('clue:list')")
    @GetMapping("/api/clues")
    public R cluePage(@RequestParam(value = "current", required = false) Integer current) {
        if (current == null) {
            current = 1;
        }
        PageInfo<TClue> pageInfo = clueService.getClueByPage(current);
        return R.OK(pageInfo);
    }

    /**
     * 导入Excel文件
     * @param file
     * @param token
     * @return
     * @throws IOException
     */
    @PreAuthorize(value = "hasAnyAuthority('clue:import')")
    @PostMapping("/api/importExcel")
    public R importExcel(MultipartFile file, @RequestHeader(value = "Authorization") String token) throws IOException {  //MultipartFile file中的file要与前端key值相同
        clueService.importExcel(file.getInputStream(), token);
        return R.OK();
    }

    /**
     * 检查手机号是否录入
     * @param phone
     * @return
     */
    @GetMapping("/api/clue/{phone}")
    public R checkPhone(@PathVariable("phone") String phone) {
        Boolean check = clueService.checkPhone(phone);
        return check ? R.OK() : R.FAIL();
    }

    /**
     * 添加线索
     * @param clueQuery
     * @param token
     * @return
     */
    @PreAuthorize(value = "hasAnyAuthority('clue:add')")
    @PostMapping("/api/clue")
    public R addClue(ClueQuery clueQuery, @RequestHeader("Authorization") String token) {
        clueQuery.setToken(token);
        int save = clueService.saveClue(clueQuery);
        return save > 0 ? R.OK() : R.FAIL();
    }

    /**
     * 编辑页面
     * @param id
     * @return
     */
    @PreAuthorize(value = "hasAnyAuthority('clue:view')")
    @GetMapping("/api/clue/detail/{id}")
    public R loadClue(@PathVariable("id") Integer id) {
        TClue tClue = clueService.getById(id);
        return R.OK(tClue);
    }

    /**
     * 编辑线索
     * @param clueQuery
     * @param token
     * @return
     */
    @PreAuthorize(value = "hasAnyAuthority('clue:edit')")
    @PutMapping("/api/clue")
    public R editClue(ClueQuery clueQuery, @RequestHeader("Authorization") String token) {
        clueQuery.setToken(token);
        int update = clueService.updateClue(clueQuery);
        return update > 0 ? R.OK() : R.FAIL();
    }

    /**
     * 删除线索
     * @param id
     * @return
     */
    @PreAuthorize(value = "hasAnyAuthority('clue:delete')")
    @DeleteMapping("/api/clue/{id}")
    public R deleteClue(@PathVariable("id") Integer id) {
        int delete = clueService.deleteClue(id);
        return delete >= 1 ? R.OK() : R.FAIL();
    }


    /**
     * 批量删除线索
     * @param ids
     * @return
     */
    @PreAuthorize(value = "hasAnyAuthority('clue:delete')")
    @DeleteMapping("/api/clue")
    public R batchDeleteClue(@RequestParam("ids") String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        int batchDel = clueService.batchDeleteClue(idList);
        return batchDel >= idList.size() ? R.OK() : R.FAIL();
    }
}
